package cn.edu.scau.cmi.ema.service.base;


import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.beans.PropertyEditorSupport;

@Component
public class CmiCurrentPaginationEditor extends PropertyEditorSupport{

	private HttpServletRequest  request;


	public  CmiCurrentPaginationEditor() {

	}

	public  CmiCurrentPaginationEditor(HttpServletRequest  request) {
		this.request = request;
	}

	@Override
	public void setAsText(String  currentPagination) {
		int actualCurrentPage = 1;
		int hiddenCurrentPagination = Integer.parseInt((String) request.getParameter("hiddenCurrentPagination"));  // 作为隐藏域设置在jsp中
		int hiddenTotalPagination = Integer.parseInt((String) request.getParameter("hiddenTotalPagination"));
		switch(currentPagination) {
		case "首页":
			actualCurrentPage = 1;
			break;
		case "上一页":
			if(hiddenCurrentPagination <= 1) {
				actualCurrentPage = 1;
			}else {
				actualCurrentPage = hiddenCurrentPagination -1;
			}
			break;
		case "下一页":
			if(hiddenCurrentPagination >= hiddenTotalPagination) {
				actualCurrentPage = hiddenCurrentPagination;
			}else {
				actualCurrentPage = hiddenCurrentPagination + 1;
			}
			break;
		case "尾页":
			actualCurrentPage = hiddenTotalPagination;
			break;
		default:
			actualCurrentPage = Integer.parseInt(currentPagination);
			break;
		}
		setValue(actualCurrentPage);
	}
}
